package com.example.springboot.controller;

import com.example.springboot.base.Result;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试全局异常
 * <p>
 * <p>
 * @author Zhang.Z
 * @version 2025-09-20
 */
@RestController
@RequestMapping(value = "/test", method = RequestMethod.GET)
public class ExceptionController {

    /**
     * 测试不通情况的异常捕捉情况
     */
    @RequestMapping(value = "/ex")
    public Result ex(Integer param) throws Exception {
        if (param == null) {
            throw new RuntimeException("参数为空");
        } else if (param == 0) {
            throw new Exception("参数值不能为0");
        }
        return Result.success("成功");
    }

    @RequestMapping(value = "/exAsModel")
    public Result exAsModel(Integer param, Model model) throws Exception {
        if (param == null) {
            throw new RuntimeException("参数为空");
        } else if (param == 0) {
            throw new Exception("参数值不能为0");
        }
        return Result.success(model.getAttribute("author"));
    }
}
